package com.jeeplus.ht.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.ht.common.FileUploadTool;
import com.jeeplus.ht.entity.FileEntity;
import com.jeeplus.ht.entity.TAttach;
import com.jeeplus.mob.common.DataConvertHelper;
import com.jeeplus.mob.common.StringHelper;
import com.jeeplus.mob.common.UploadQiNiu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ht/attach")
@Api(tags ="附件管理")
public class AttachController {

    /**
     * <附件上传></附件上传>
     * @param
     * @return
     */
    @ApiOperation(value = "附件上传")
    @PostMapping("upload")
    public AjaxJson upload(HttpServletRequest request, HttpServletResponse response,
                                 @RequestParam(value = "file", required = false) MultipartFile file,
                                 @RequestParam(value = "file1", required = false) MultipartFile file1,
                                 @RequestParam(value = "file2", required = false) MultipartFile file2,
                                 @RequestParam(value = "file3", required = false) MultipartFile file3,
                                 @RequestParam(value = "file4", required = false) MultipartFile file4,
                                 @RequestParam(value = "file5", required = false) MultipartFile file5,
                                 @RequestParam(value = "file6", required = false) MultipartFile file6,
                                 @RequestParam(value = "file7", required = false) MultipartFile file7,
                                 @RequestParam(value = "file8", required = false) MultipartFile file8,
                                 @RequestParam(value = "file9", required = false) MultipartFile file9,
                                 @RequestParam(value = "file10", required = false) MultipartFile file10,
                                 TAttach tattach) {
        Map<String, Object> dataMap = DataConvertHelper.getRequestParamsNoToken(request);
        FileUploadTool fileUploadTool = new FileUploadTool();
        FileEntity entity = new FileEntity();
        List<Map<String,Object>> maps = new ArrayList<>();
        if (file != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file1 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file1, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file2 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file2, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file3 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file3, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file4 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file4, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file5 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file5, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file6 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file6, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file7 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file7, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file8 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file8, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file9 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file9, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        if (file10 != null) {
            Map<String, Object> map = new HashMap<>();
            entity = fileUploadTool.createFile(file10, request);
            if (entity != null) {
                tattach.setAttachId(StringHelper.GetGUID());
                tattach.setAttachPath(entity.getPath());
                map.put("url",entity.getPath());
                map.put("alt","");
                map.put("href","");
                maps.add(map);
            } else {
                return AjaxJson.error("上传失败");
            }
        }
        return    AjaxJson.success().put("data",maps);
    }

    /**
     * <七牛上传>
     * @param
     * @return
     */
    @ApiOperation(value = "附件上传")
    @PostMapping("qnUpload")
    public AjaxJson qnUpload(HttpServletRequest request, HttpServletResponse response,
                                   @RequestParam(value = "file", required = false) MultipartFile file,
                                   @RequestParam(value = "file1", required = false) MultipartFile file1,
                                   @RequestParam(value = "file2", required = false) MultipartFile file2,
                                   @RequestParam(value = "file3", required = false) MultipartFile file3,
                                   @RequestParam(value = "file4", required = false) MultipartFile file4,
                                   @RequestParam(value = "file5", required = false) MultipartFile file5,
                                   @RequestParam(value = "file6", required = false) MultipartFile file6,
                                   @RequestParam(value = "file7", required = false) MultipartFile file7,
                                   @RequestParam(value = "file8", required = false) MultipartFile file8,
                                   @RequestParam(value = "file9", required = false) MultipartFile file9,
                                   @RequestParam(value = "file10", required = false) MultipartFile file10
    ) throws Exception {
        Map<String, Object> dataMap = DataConvertHelper.getRequestParamsNoToken(request);
        List<Map<String,Object>> maps = new ArrayList<>();
        if (file != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file1 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file1);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file2 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file2);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file3 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file3);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file4 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file4);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file5 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file5);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file6 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file6);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file7 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file7);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file8 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file8);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file9 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file9);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        if (file10 != null) {
            Map<String, Object> map = new HashMap<>();
            File files = FileUploadTool.multipartFileToFile(file10);
            byte[] f = UploadQiNiu.readToByte(files);
            String filepath = UploadQiNiu.byteUploadFile(f);
            map.put("url",filepath);
            map.put("alt","");
            map.put("href","");
            maps.add(map);
        }
        return AjaxJson.success().put("data",maps);
    }

    /**
     * <附件删除>
     * @param
     * @return
     */
    @ApiOperation(value = "附件删除")
    @PostMapping("delFile")
    public AjaxJson delFile(HttpServletRequest request,TAttach tattach) {
        if(!StringHelper.IsEmptyOrNull(tattach.getAttachId()) || !StringHelper.IsEmptyOrNull(tattach.getBussId())) {
            FileUploadTool fileUploadTool = new FileUploadTool();
            fileUploadTool.delAttach(tattach.getAttachPath(),request);
        }
        return AjaxJson.success("操作成功");
    }

    /**
     * <七牛附件删除>
     * @param
     * @return
     */
    @ApiOperation(value = "附件删除")
    @PostMapping("qnDelFile")
    public AjaxJson qnDelFile(HttpServletRequest request,String key) {
        UploadQiNiu.del(key);
        return AjaxJson.success("操作成功");
    }

}
